
#import "PUCollcetVC.h"
#import "JXCategoryView.h"
#import "PUCollcetListVC.h"
@interface PUCollcetVC ()<UIScrollViewDelegate,JXCategoryViewDelegate,JXCategoryListContainerViewDelegate>
@property (nonatomic, strong) UIScrollView          *scrollView;
@property (nonatomic, strong) NSArray               *titles;
@property (nonatomic, strong) NSMutableArray        *childVCs;
@property (nonatomic, strong) JXCategoryTitleView   *categoryView;
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
@property (nonatomic, strong) NSArray *loveLateAppTitles;
@property (nonatomic, strong) NSArray *loveLateAppPages;
@end

@implementation PUCollcetVC

- (UIView *)listView {
    return self.view;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    !self.scrollCallback ?: self.scrollCallback(scrollView);
}

#pragma mark - JXPagingViewListViewDelegate

//- (UIScrollView *)listScrollView {
//    return self.loveLateAppScrolview;
//}

- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
    self.scrollCallback = callback;
}

//-(void)viewDidAppear:(BOOL)animated{
//    [super viewDidAppear:animated];
//    self.navigationController.navigationBar.translucent = YES;
//    if (@available(iOS 15.0, *)) {
//        UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
//        barApp.backgroundColor = [UIColor clearColor];
//        barApp.backgroundEffect = nil;
//        barApp.shadowColor = nil;
//        self.navigationController.navigationBar.scrollEdgeAppearance = barApp;
//        self.navigationController.navigationBar.standardAppearance = barApp;
//    }else{
//        [self.navigationController.navigationBar setShadowImage:[UIImage new]];
//        [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    }
//}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}


- (void)dealloc {
    [NSNotificationCenter.defaultCenter removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = UIColor.whiteColor;
    
    UIImageView *img=[[UIImageView alloc]init];
    img.backgroundColor=[UIColor whiteColor];
    img.image=[UIImage imageNamed:@"我的背景"];
    [self.view addSubview:img];
    
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(0);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
       
    }];
    
    
    self.loveLateAppTitles = @[@"收藏", @"历史"];
    
    PUCollcetListVC *vc1=[[PUCollcetListVC alloc]init];
    vc1.type=0;
    
    PUCollcetListVC *vc2=[[PUCollcetListVC alloc]init];
    vc2.type=1;
    self.loveLateAppPages = @[vc1,vc2];
    
    [self.view addSubview:self.categoryView];
    [self.view addSubview:self.listContainerView];
    self.listContainerView.backgroundColor=[UIColor clearColor];
    [self loveLateAppConfigView];

}


- (void)loveLateAppConfigView {
    
    self.categoryView.frame = CGRectMake(-15, [self tz_safeAreaInsets].top+6, 150, 44);
    self.categoryView.titles = self.loveLateAppTitles;
    self.listContainerView.frame = CGRectMake(0, [self tz_safeAreaInsets].top + 44+10+6, _window_width, _window_height - [self tz_safeAreaInsets].top - 44 - [self tz_safeAreaInsets].bottom - 49-16);
    [self.listContainerView reloadData];
    [self.categoryView reloadData];
    
}

- (UIEdgeInsets)tz_safeAreaInsets {
    UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
    if (![window isKeyWindow]) {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        if (CGRectEqualToRect(keyWindow.bounds, [UIScreen mainScreen].bounds)) {
            window = keyWindow;
        }
    }
    if (@available(iOS 11.0, *)) {
        UIEdgeInsets insets = [window safeAreaInsets];
        return insets;
    }
    return UIEdgeInsetsZero;
}

#pragma mark - LateLoveAppJXViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
}

- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
}

#pragma mark - LateLoveAppJXListContainerViewDelegate
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.loveLateAppTitles.count;
}

- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {

    return self.loveLateAppPages[index];
}


#pragma mark - lazy UI
- (JXCategoryTitleView *)categoryView {
    if (!_categoryView) {
        _categoryView = [[JXCategoryTitleView alloc] init];
        _categoryView.defaultSelectedIndex = 0;
        _categoryView.delegate = self;
        _categoryView.titles = self.loveLateAppTitles;
    

        _categoryView.titleFont = [UIFont systemFontOfSize:16];
        _categoryView.titleSelectedFont = [UIFont boldSystemFontOfSize:16];
        _categoryView.titleColor = [UIColor colorWithHex:0x333333];
        _categoryView.titleSelectedColor = [UIColor colorWithHex:0x333333];
        _categoryView.titleLabelAnchorPointStyle = JXCategoryTitleLabelAnchorPointStyleBottom;
        JXCategoryIndicatorLineView *loveLateAppLineView = [[JXCategoryIndicatorLineView alloc] init];
        loveLateAppLineView.indicatorWidth = 20;
        loveLateAppLineView.indicatorHeight = 5;
        loveLateAppLineView.indicatorColor = [UIColor colorWithHex:0xFF729A];
        loveLateAppLineView.verticalMargin = 3;
        _categoryView.indicators = @[loveLateAppLineView];
        
        _categoryView.listContainer = self.listContainerView;
    }
    return _categoryView;
}

- (JXCategoryListContainerView *)listContainerView {
    if (!_listContainerView) {
        _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    }
    return _listContainerView;
}

@end


